package pl.sar.auth.server.ejb.entities;

import java.io.Serializable;
import java.util.Date;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table( name = "USERS" )
public class User implements Serializable
{

    private static final long serialVersionUID = -5528113503853896654L;

    @Id
    @GeneratedValue( strategy = GenerationType.IDENTITY )
    private long id;

    @Column( name = "name", unique = true, nullable = false )
    @Basic( optional = false )
    private String name;

    @Column( name = "firstname" )
    private String firstName;

    @Column( name = "password" )
    private String password;

    @Column( name = "mail", unique = true, nullable = false )
    private String mail;

    @Column( name = "validfrom" )
    @Temporal( TemporalType.TIMESTAMP )
    private Date validFrom;

    @Column( name = "valid" )
    private boolean valid;

    @ManyToOne(optional = false)
    @JoinColumn(name="project_id", nullable = false)
    private Project project;

    @ManyToMany( cascade = CascadeType.ALL )
    @JoinTable( name = "users_roles", joinColumns =
    { @JoinColumn( name = "USER_ID", referencedColumnName = "ID" ) }, inverseJoinColumns =
    { @JoinColumn( name = "ROLE_ID", referencedColumnName = "ID" ) } )
    private Set< Role > roles;

    public User()
    {

    }

    public long getId()
    {
        return id;
    }

    public String getName()
    {
        return name;
    }

    public void setName( String name )
    {
        this.name = name;
    }

    public String getFirstName()
    {
        return firstName;
    }

    public void setFirstName( String firstName )
    {
        this.firstName = firstName;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword( String password )
    {
        this.password = password;
    }

    public String getMail()
    {
        return mail;
    }

    public void setMail( String mail )
    {
        this.mail = mail;
    }

    public Date getValidFrom()
    {
        return validFrom;
    }

    public void setValidFrom( Date validFrom )
    {
        this.validFrom = validFrom;
    }

    public boolean isValid()
    {
        return valid;
    }

    public void setValid( boolean valid )
    {
        this.valid = valid;
    }

    public void setId( long id )
    {
        this.id = id;
    }

    public Project getProject()
    {
        return project;
    }

    public void setProject( Project project )
    {
        this.project = project;
    }

    public Set< Role > getRoles()
    {
        return roles;
    }

    public void setRoles( Set< Role > roles )
    {
        this.roles = roles;
    }
}
